﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.IO;
using University.Workshop.Business;


namespace University.Workshop.WinApp
{
    public partial class Reports : TemplateForm
    {
        

        public Reports()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!BookingManager.ValidDateCheckInCheckout(firstDateTimePicker.Value, lastDateTimePicker.Value))
            {
                MessageBox.Show("Invalid Dates");

            }
            else
            {
                Document document = new Document(PageSize.LETTER, 50, 40, 40, 40);

                //PdfWriter.GetInstance(document, new FileStream(@"D:Reporte"+DateTime.Now.ToShortTimeString()+".pdf", FileMode.OpenOrCreate));
                String ReportName = DateTime.Now.ToLongTimeString().Substring(0, 9).Replace(":", "-");
                PdfWriter.GetInstance(document, new FileStream(@"D:Reporte" + ReportName + ".pdf", FileMode.OpenOrCreate));
                document.Open();
                //string imageFilePath = "HotelRealDeBarco1.png";
                iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(this.panel1.BackgroundImage, BaseColor.LIGHT_GRAY);
                //Resize image depend upon your need
                jpg.ScaleToFit(240f, 220f);
                //Give space before image
                jpg.SpacingBefore = 30f;
                //Give some space after the image
                jpg.SpacingAfter = 1f;
                jpg.Alignment = Element.ALIGN_CENTER;
                document.Add(jpg);
                try
                {
                    Paragraph paragraph = new Paragraph();
                    Chunk chunk1 = new Chunk(printTitle(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 18, iTextSharp.text.Font.UNDERLINE));
                    paragraph.Add(chunk1);
                    //Chunk image= new Chunk(new iTextSharp.text.Image(new Uri(".\\Resources\HotelRealDeBarco1.png")),);
                   //Paragraph paragraph0 = new Paragraph(image);
                    Chunk chunk2 = new Chunk("Cochabamba, " + DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, iTextSharp.text.Font.NORMAL));
                    Paragraph paragraph1 = new Paragraph();
                    paragraph1.Add(chunk2);
                    paragraph.Alignment = Element.ALIGN_CENTER;
                    paragraph1.Alignment = Element.ALIGN_LEFT;

                    document.Add(paragraph);
                    //document.Add(new Paragraph("\n\r"));
                    //document.Add(paragraph0);
                    document.Add(new Paragraph("\n\r"));
                    document.Add(paragraph1);
                    document.Add(new Paragraph("\n\r"));
                    //ClientReport cli = new ClientReport();
                    PdfPCell cell = new PdfPCell();

                    cell.Colspan = 3;
                    cell.Rowspan = 1;
                    cell.Padding = 1;
                    //cell.PaddingLeft = 1;
                    //cell.PaddingRight = 1;

                    cell.HorizontalAlignment = 1; //0=Left, 1=Center, 2=Right
                    iTextSharp.text.Font titulos = FontFactory.GetFont(FontFactory.HELVETICA, 11, iTextSharp.text.Font.BOLD);
                    if (radioButtonClientes.Checked == true)
                    {
                        PdfPTable table = new PdfPTable(6);
                        DataTable resultado = ClientReport.LoadClient();
                        table.AddCell(new Phrase("FULLNAME", titulos));
                        table.AddCell(new Phrase("ID", titulos));
                        table.AddCell(new Phrase("PHONE", titulos));
                        table.AddCell(new Phrase("E-MAIL", titulos));
                        table.AddCell(new Phrase("ORG.", titulos));
                        table.AddCell(new Phrase("COUNTRY", titulos));
                        for (int a = 0; a < resultado.Rows.Count; a++)
                        {
                            table.AddCell(resultado.Rows[a].ItemArray[0].ToString());
                            table.AddCell(resultado.Rows[a].ItemArray[1].ToString());
                            table.AddCell(resultado.Rows[a].ItemArray[2].ToString());
                            table.AddCell(resultado.Rows[a].ItemArray[3].ToString());
                            table.AddCell(resultado.Rows[a].ItemArray[4].ToString());
                            table.AddCell(resultado.Rows[a].ItemArray[5].ToString());
                        }
                        document.Add(table);
                    }
                    else
                    {
                        if (radioButtonHabitacionesOcupadas.Checked == true)
                        {
                            PdfPTable table = new PdfPTable(4);
                            DataTable resultado = ClientReport.TakenRooms();
                            table.AddCell(new Phrase("Room ID", titulos));
                            table.AddCell(new Phrase("Full Name", titulos));
                            table.AddCell(new Phrase("Check-in", titulos));
                            table.AddCell(new Phrase("Check-out", titulos));
                            for (int a = 0; a < resultado.Rows.Count; a++)
                            {
                                
                                table.AddCell(RoomStuffManager.GetRoomNameByRoomId(resultado.Rows[a].ItemArray[0].ToString()));
                                table.AddCell(ClientManager.GetFullNameFromCI(resultado.Rows[a].ItemArray[1].ToString()));
                                table.AddCell(resultado.Rows[a].ItemArray[2].ToString().Substring(0,10));
                                table.AddCell(resultado.Rows[a].ItemArray[3].ToString().Substring(0,10));
                            }
                            document.Add(table);
                        }
                        else
                        {
                            if (radioButtonEmpleados.Checked == true)
                            {
                                PdfPTable table = new PdfPTable(4);
                                DataTable resultado = ClientReport.Employees();
                                table.AddCell(new Phrase("CI Employee", titulos));
                                table.AddCell(new Phrase("First Name", titulos));
                                table.AddCell(new Phrase("Last Name", titulos));
                                table.AddCell(new Phrase("Work Area", titulos));
                                for (int a = 0; a < resultado.Rows.Count; a++)
                                {
                                    table.AddCell(resultado.Rows[a].ItemArray[0].ToString());
                                    table.AddCell(resultado.Rows[a].ItemArray[1].ToString());
                                    table.AddCell(resultado.Rows[a].ItemArray[2].ToString());
                                    table.AddCell(resultado.Rows[a].ItemArray[3].ToString());
                                }
                                document.Add(table);
                            }
                        }
                    }



                    document.Close();
                    System.Diagnostics.Process.Start("D:Reporte" + ReportName + ".pdf");
                }
                catch(Exception ex){
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private string  printTitle()
        {
            if (radioButtonClientes.Checked == true)
            {
                return "Hotel - Client Report";
            }
            else
            {
                if (radioButtonHabitacionesOcupadas.Checked == true)
                {
                    return "Hotel - Taken Room Report";
                }
                else
                {
                    if (radioButtonEmpleados.Checked == true)
                    {
                        return "Hotel - Employees Report";
                    }
                    else
                    {
                        if (radioButtonClientesFrecuentes.Checked == true)
                        {
                            return"Hotel - Clientes Frecuentes Report";
                        }
                        else
                        {
                            if (radioButtonReservas.Checked == true)
                            {
                                return"Hotel - Booked Rooms Report";
                            }
                            else
                            {
                                throw new ArgumentException("Invalid Input Parameters");
                            }
                        }
                    }
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
